package arithmetic.LeetCode.Max_Area_of_Island;


/**
 * @author zhangyanqi
 * @since 1.0 2017/10/10
 */
public class Solution {

    public static int maxAreaOfIsland(int[][] grid) {
        int max = 0;
        int lengthA = grid.length;
        int lengthB = grid[0].length;
        int[][] mark = new int[lengthA][lengthB];
        for (int i = 0; i < lengthA; i++) {
            for (int j = 0; j < lengthB; j++) {
                if (grid[i][j] == 1 && mark[i][j] == 0) { //该点是1,且被标记为未被扫描
                    int temp = search(grid, mark, i, j, 0);
                    if (temp > max) {
                        max = temp;
                    }
                }
            }
        }
        return max;
    }

    public static int search(int[][] grid, int[][] mark, int a, int b, int area) {
        int lengthA = grid.length;
        int lengthB = grid[0].length;
        if (0 <= a && 0 <= b && a < lengthA && b < lengthB && mark[a][b] == 0 && grid[a][b] == 1) {
            area++;
            mark[a][b] = 1;
        } else {
            return area;
        }
        area = search(grid, mark, a+1, b, area);
        area = search(grid, mark, a-1, b, area);
        area = search(grid, mark, a, b+1, area);
        area = search(grid, mark, a, b-1, area);
        return area;
    }


    public static void main(String[] args) {
        int[][] test = {{0, 1}, {1, 1}};
        int i = maxAreaOfIsland(test);
        System.out.println(i);
    }

}
